<template>
    <div class="Userdata">
        <!-- -------------------------------个人资料 -->
        <div class="back-nav-box">
            <back-nav title="个人资料" left-text="返回" left-arrow @navback='navback'></back-nav>
        </div>
        <!-- 蓝色底 -->
        <div class="blue-box"></div>
        <!-- 信息栏 -->
        <div class="userdata-msg">
            <ul>
                <li ><div >头像</div><div @click="newHeadPic"><img :src="userImg" class="user-pic" alt=""></div></li>
                <li><div>用户ID</div><div><span class="grayfont">{{userdata.userId}}</span></div></li>
                <li @click="newNickName"><div >昵称</div><div><span class="grayfont">{{userdata.nickName}}</span></div></li>
                <li @click="newIntro"><div>简介</div><div><span class="grayfont">{{userdata.desc==""?'这个人很懒，什么也没留下':userdata.desc}}</span></div></li>
            </ul>
        </div>

        <!-- 改昵称弹窗 -->
        <div class="tanchuang">
            <van-popup v-model="popupshow" position="bottom" :style="{ height: '20%' }">
                <van-form >
                    <van-field
                    v-model="newNick"
                        name="新昵称"
                        label="新昵称"
                        placeholder="新昵称"
                        :rules="[{ required: true, message: '请填写新昵称' }]"
                    />
                </van-form>
                <div class="submit-btn" @click="submitNickname"><van-button round type="info">提&nbsp;&nbsp;交</van-button></div>
            </van-popup>
        </div>
        <!-- 改简介弹窗 -->
         <div class="tanchuang">
            <van-popup v-model="isshow" position="bottom" :style="{ height: '20%' }">
                <van-form >
                    <van-field
                    v-model="UserIntro"
                        name="简介"
                        label="简介"
                        placeholder="简介"
                        :rules="[{ required: true, message: '请填写简介' }]"
                    />
                </van-form>
                <div class="submit-btn" @click="submitIntro"><van-button round type="info">提&nbsp;&nbsp;交</van-button></div>
            </van-popup>
        </div>

        <!-- 上传头像 -->
        <div class="upload" v-show="showHeadpic==true">
            <div class="closeUpload" @click="closeUpload"><van-icon name="close" /></div>
            <div>
                <van-uploader :after-read="afterRead" />
            </div>
        </div>
    </div>
</template>

<script>
import { Dialog } from "vant";
import BackNav from '../components/BackNav.vue'
    export default {
        data() {
            return {
                userdata:{},
                userImg:'',
                popupshow:false,
                isshow:false,
                newNick:'',
                UserIntro:'',
                showHeadpic:false,
            }
        },
        components:{BackNav},
        created() {
            this.userData();
        },
        methods: {
            // vant上传文件
            afterRead(file) {
            // 此时可以自行将文件上传至服务器
                console.log(file);
                let headImgData=file
                // 数据处理
                let imgType='png'
                // console.log('base64',headImgData.content);
                let serverBase64Img=headImgData.content.replace(/^data:image\/[A-Za-z]+;base64,/, '')
                    console.log('base64',serverBase64Img);
                this.axios.post('/updateAvatar',{imgType,serverBase64Img}).then(res=>{
                           console.log('res==>',res);
                           this.$toast(res.data.msg)
                          setTimeout(() => {
                            this.showHeadpic=false;
                            this.userData();
                            this.userImg=res.data.userImg
                          }, 1000);
                       }).catch(err=>{
                           console.log('err',err);
                       })
            },
            // 关闭
            closeUpload(){
                this.showHeadpic=false
                console.log('点击关闭头像');
            },
            // 修改头像
            newHeadPic(){
                this.showHeadpic=true
            },
            // 简介提交按钮
            submitIntro(){
                  this.axios.post('/updateDesc',{desc:this.UserIntro}).then(res=>{
                           console.log('res==>',res);
                           this.$toast(res.data.msg)
                          setTimeout(() => {
                            this.isshow=false;
                            this.userData();
                          }, 1000);
                       }).catch(err=>{
                           console.log('err',err);
                       })
            },
            // 昵称提交按钮
            submitNickname(){
                this.axios.post('/updateNickName',{nickName:this.newNick}).then(res=>{
                           console.log('res==>',res);
                           this.$toast(res.data.msg)
                          setTimeout(() => {
                            this.popupshow=false;
                            this.userData();
                          }, 1000);
                       }).catch(err=>{
                           console.log('err',err);
                       })
            },
            newNickName(){
                        Dialog.confirm({
                        title: '修改昵称',
                        message: '您要修改昵称吗？',
                    })
                        .then(() => {
                        // on confirm
                        this.popupshow=true
                       
                })
                .catch(() => {
                // on cancel
                });
               
            },
            newIntro(){
                     Dialog.confirm({
                        title: '修改简介',
                        message: '您要修改简介吗？',
                    })
                        .then(() => {
                        // on confirm
                        this.isshow=true
                       
                })
                .catch(() => {
                // on cancel
                });
            },
            // 返回
            navback(){
                this.$router.go(-1)
            },
             // 个人资料
            userData(){
            this.axios({
                method:'get',
                url:'/findAccountInfo',
            }).then(res=>{
                console.log('个人资料res=>',res);
                this.userdata=res.data.result[0]
                this.userImg=res.data.result[0].userImg
                console.log( this.userImg);
            }).catch(err=>{
                console.log('err',err);
            })
            },
        },
    }
</script>

<style lang="less" scoped>
/deep/.van-uploader__upload{
    width: 100px;
    height: 100px;
}
.upload{
    width: 100px;
    height: 100px;
    background-color: aquamarine;
    position: fixed;
    top: 100px;
    left: 138px;
    .closeUpload{
        position: absolute;
        width: 20px;
        height: 20px;
        text-align: center;
        top: 5px;
        right: 0px;
        z-index:9999;
        font-size: 14px;
    }
}
.tanchuang{
    width: 355px;
    height: 200px;
    padding:0 10px;
    box-sizing: border-box;
}
    /deep/.van-cell{
        height: 60px;
        line-height: 60px;
    }
.submit-btn{
    width: 355px;
    margin: 20px auto 0 auto;
      .van-button {
        width:100%;
        margin: 0 auto;
        background-color: #0c34ba;
      }
  }
.grayfont{
    font-size: 14px;
    color: #666;
    float: right;
}
.Userdata{
    min-height: 100vh;
    background-color: #f7f7f7;
}
// 信息栏
.userdata-msg{
    width: 350px;
    // height: 350px;
    position: relative;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    position: fixed;
    top: 160px;
    left: 12.5px;
    background-color: #fff;
    padding: 10px;
    padding-bottom: 0px;
    box-sizing: border-box;
    // 
    li{
        display: flex;
        height: 60px;
        // line-height: 40px;
        align-items: center;
        border-bottom: 2px solid rgb(226, 224, 224);
        // margin-bottom: 20px;
        font-size: 16px;
        & div:first-child{
            flex: 1;
        }
        & div:last-child{
            flex: 2;
        }
        &:last-child{
            border-bottom: 0px;
        }
        // 头像
        .user-pic{
                width: 35px;
                height: 35px;
                border-radius: 50%;
                float: right;
        }
    }
}
.back-nav-box{
        z-index: 9;
        position: sticky;
        top: 0;
    }
    // 蓝色底
    .blue-box{
        width: 100%;
        height: 150px;
        background-color: #0c34ba;
    }
</style>